package ray.wx.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ray.wx.util.MysqlUtil;

/**
 * Servlet implementation class KeyAddServlet
 */
@WebServlet("/KeyAddServlet")
public class KeyAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public KeyAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        String strKey1 = request.getParameter("key");
        String strKey = URLDecoder.decode(strKey1,"utf-8");
        String strReply1 = request.getParameter("reply");
        String strReply = URLDecoder.decode(strReply1,"utf-8");
        
        MysqlUtil mysql = new MysqlUtil(MysqlUtil.dbUrl, MysqlUtil.dbUser, MysqlUtil.dbPass);
        try {
			mysql.connect();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		    return;
			//throw new ServletException("Database init failed!");
		}
        
        String sql = "insert into key_reply_msg (input_key, reply) values ('" + strKey + "', '" + strReply + "');";
        mysql.update_db(sql);
        
		PrintWriter out = response.getWriter();
		out.print("");
		
	    out.close();  
	    out = null;		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
